package com.javasm.crm.service.impl.productimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javasm.crm.domin.criteria.TypeCriteria;
import com.javasm.crm.domin.entity.productEntity.Brand;
import com.javasm.crm.domin.entity.productEntity.Color;
import com.javasm.crm.domin.entity.productEntity.Type;
import com.javasm.crm.mapper.goodsmapper.ColorMapper;
import com.javasm.crm.mapper.goodsmapper.TypeMapper;
import com.javasm.crm.service.productservice.ColorService;
import com.javasm.crm.service.productservice.TypeService;
import com.javasm.crm.utils.page.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Author：zhangyang
 * @Version：1.0
 * @Date：2021/7/13-10:41
 * @desp:
 * @since:
 */
@Service
@Transactional
public class TypeServiceImpl extends ServiceImpl<TypeMapper,Type> implements TypeService {
    @Autowired
    TypeMapper typeMapper;
    @Override
    public List<Type> getAllTypes() {
        return typeMapper.selectList(null);
    }

    @Override
    public PageResult<Type> searchPageByCriteria(TypeCriteria typeCriteria) {
        Integer currentPage = typeCriteria.getCurrentPage();

        Integer pageSize = typeCriteria.getPageSize();
        if (StringUtils.isEmpty(currentPage)) {
            currentPage=1;
        }
        if (StringUtils.isEmpty(pageSize)) {
            pageSize=5;
        }

        QueryWrapper<Type> exampleQueryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(typeCriteria.getTypeName())){
            exampleQueryWrapper.like("type_name",typeCriteria.getTypeName());
        }
        Page<Type> examplePage = new Page<>(currentPage, pageSize);
        List<Type> queryall = typeMapper.queryall(examplePage, exampleQueryWrapper);

        return new PageResult<Type>(examplePage.getTotal(), queryall);
    }

    @Override
    public int update(Type t) {
        return typeMapper.updateById(t);
    }

    @Override
    public int add(Type t) {
        return typeMapper.insert(t);
    }

    @Override
    public int delete(Integer id) {
        return typeMapper.deleteById(id);
    }

}
